package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.listener.OnBackCallback;
import com.axend.aerosense.room.listener.OnSwitchChageListener;
import com.axend.aerosense.room.ui.activity.RoomVipFunActivity;
import com.axend.aerosense.room.ui.fragment.RoomSettingFragment;
import com.axend.aerosense.room.viewmodel.RoomVipFunSaveDataViewModel;
import com.blankj.utilcode.util.LogUtils;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.common.ui.CustomTipsDialog;
import com.axend.aerosense.room.R;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * vip功能基类
 * Author: ChenHongWei
 * Date: 2021/3/23 11:09
 */
public abstract class BaseVipFunFragment<V extends ViewDataBinding>
        extends MvvmBaseFragment<V, RoomVipFunSaveDataViewModel>
        implements OnSwitchChageListener, OnBackCallback {

    private CustomTipsDialog tipsDialog;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //功能开关监听
        getCurrentActivity().setSwitchChageListener(this);
        //返回回调
        getCurrentActivity().setOnBackCallback(this);


        //getCurrentActivity().isChangeData();

        viewModel.setActivity(getActivity());
    }

    @Override
    protected RoomVipFunSaveDataViewModel getViewModel() {
        return new ViewModelProvider(this).get(RoomVipFunSaveDataViewModel.class);
    }

    /**
     * 返回时显示提示窗口
     * @return
     */
    @Override
    public boolean onBack() {
        if (getCurrentActivity().isChange()){
            LogUtils.d("保存设置");
            saveSettings();
            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
//            getCurrentActivity().setResult(RoomSettingFragment.REQUEST_VIP_SET_CODE, getCurrentActivity().getIntent());
//            tipsDialog = new CustomTipsDialog(getContext(), getString(R.string.room_is_save_setting));
//            tipsDialog.setOnClickBottomListener(new CustomTipsDialog.OnClickBottomListener() {
//                @Override
//                public void onPositiveClick() {
//                    tipsDialog.dismiss();
//                    LogUtils.d("保存设置");
//                    saveSettings();
//                    getCurrentActivity().setResult(RoomSettingFragment.REQUEST_VIP_SET_CODE, getCurrentActivity().getIntent());
//                }
//
//                @Override
//                public void onNegtiveClick() {
//                    tipsDialog.dismiss();
//                    LogUtils.d("不保存设置");
//                    getCurrentActivity().onBackPressed();
//                }
//            });
//            tipsDialog.show();
        }else {
            getCurrentActivity().onBackPressed();
        }
        return true;
    }

    /** 保存设置 */
    protected void saveSettings(){

    }

    protected RoomVipFunActivity getCurrentActivity(){
        if (getActivity() instanceof RoomVipFunActivity){
            return ((RoomVipFunActivity) getActivity());
        }else {
            return null;
        }
    }

    @CallSuper
    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {

    }

    /** 获取设置Bean */
    protected RoomVipSetBean getVipSettings(){
        return (RoomVipSetBean) getCurrentActivity().getIntent().getSerializableExtra(RoomVipFunActivity.FLAG_SETTINGS_DATA);
    }

    /** 获取房间UUID */
    protected String getRoomUuid(){
        return getVipSettings().getRoomUuid();
    }

    /** 获取房间类型 */
    protected int getRoomType(){
        return getVipSettings().getRoomType();
    }

    protected void setTitle(int id){
        getCurrentActivity().setTitle(getString(id));
    }

    protected int getNum(String text){
        String temp = "";
        for (int i = 0; i < text.length(); i++){
            if (text.charAt(i) >= 48 && text.charAt(i) <= 57){
                temp += text.charAt(i);
            }
        }
        return Integer.parseInt(temp);
    }

    /**
     * 生成步长为0.5小时的时间列表
     * @param startTime  开始时间
     * @param endTime  结束时间
     * @param step  步长
     * @return
     */
    public static Map<String, Float> getTimeList(float startTime, float endTime, float step, String unit){
        Map<String, Float> map = new LinkedHashMap<>();
        map.put(startTime + unit, startTime);
        for (float i = startTime; i < endTime; i+= step){
            map.put(i + unit, i);
        }
        map.put(endTime + unit, endTime);
        return map;
    }

    /** 设置更改 */
    protected void change(){
        getCurrentActivity().setChange(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (tipsDialog != null && tipsDialog.isShowing()){
            tipsDialog.dismiss();
        }
    }
}
